<?php
class Comment extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 获取评论审核列表
     * name: index
     * @throws Exception
     * @Author:taiyang
     */
    public function index(){
        $post_id=$this->input->get_post('post_id')?$this->input->get_post('post_id'):'';
        $post_type=$this->input->get_post('post_type')?$this->input->get_post('post_type'):"";
        $s_type_id=$this->input->get_post('s_type_id')?$this->input->get_post('s_type_id'):'';
        $s_content=$this->input->get_post('s_content')?$this->input->get_post('s_content'):'';
        $status=$this->input->get_post('status')?$this->input->get_post('status'):'';
        $is_audit=$this->input->get_post('is_audit')?$this->input->get_post('is_audit'):'';
        $is_audio=$this->input->get_post('is_audio')?$this->input->get_post('is_audio'):'';
        $page=$this->input->get_post('page')?$this->input->get_post('page'):1;
        $pagesize=$this->input->get_post('pagesize')?$this->input->get_post('pagesize'):10;
        $data=array(
            'post_id'=>$post_id,
            's_type_id'=>$s_type_id,
            's_content'=>$s_content,
            'post_type'=>$post_type,
            'is_audit'=>$is_audit,
            'is_audio'=>$is_audio,
            'status'=>$status,
        );
        $list=$this->CommentModel->getCommentList($data,$page,$pagesize,'tc.add_time desc');
        $num=$this->CommentModel->getCommentNum($data);
        //分页
        $url = 'index.php?c=comment&m=index&post_id='.$post_id.'&s_type_id='.$s_type_id.'&s_content='.$s_content.'&post_type='.$post_type.'&is_audit='.$is_audit.'&is_audio='.$is_audio.'&status='.$status;
        $multipage = multipage($num, $page, $pagesize, $url);
        $pageTitle = "评论管理";
        $this->load->view('comment/index',array(
            "list" => $list,
            "pageTitle" => $pageTitle,
            "post_id" => $post_id,
            "post_type" => $post_type,
            "is_audit" => $is_audit,
            "is_audio" => $is_audio,
            "s_type_id" => $s_type_id,
            "status" => $status,
            "s_content" => $s_content,
            "multipage" => $multipage,
        ));
    }

    /**
     * 删除评论
     * name: del_comment
     * @throws Exception
     * @Author:taiyang
     */
    public function del_comment(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        $tag_id=$this->input->get_post('tag_id')?$this->input->get_post('tag_id'):'';
        $type=$this->input->get_post('type')?$this->input->get_post('type'):1;//1删除否则为恢复
        if(empty($id)){
            echo json_encode(array('status'=>-1,'msg'=>'操作错误……'));exit;
        }
        $status=($type == 1?'-1':"1");
        $is_audit=($type == 1?'1':"0");
        $res=$this->CommentModel->update_data(array('status'=>$status,'is_audit'=>$is_audit),array('id'=>$id));
        if($res !== false){
            $this->CommentModel->update_count($id,$type);
            $reportTag = $this->ReportModel->tag;
            $comment_data=$this->CommentModel->query_one(array('id'=>$id));
            $userData = getLoginUser();
            //通知
            $this->NoticeModel->send_notice($comment_data['uid'], $userData['uid'], 3, 2, $id, $reportTag[$tag_id], '');
        }
        echo json_encode(array('status'=>1,'msg'=>'删除成功！'));exit;

    }

    /**
     * 评论审核成功
     * name: comment_success
     * @throws Exception
     * @Author:taiyang
     */
    public function comment_success(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        if(empty($id)){
            echo json_encode(array('status'=>-1,'msg'=>'评论内容不能为空……'));exit;
        }
        $res=$this->CommentModel->update_data(array('status'=>1,'is_audit'=>1),array('id'=>$id));
        echo json_encode(array('status'=>1,'msg'=>'评论审核成功！'));exit;
    }

    /**
     * 更新评论
     * name: up_desc
     * @throws Exception
     * @Author:taiyang
     */
    public function up_desc(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        $desc=$this->input->get_post('desc')?$this->input->get_post('desc'):'';
        if(empty($id) || empty($desc)){
            echo json_encode(array('status'=>-1,'msg'=>'评论内容不能为空……'));exit;
        }
        $this->CommentModel->update_data(array('desc'=>$desc),array('id'=>$id));
        //添加修改人信息
        $this->AdminOpenationModel->insertLog($id,5,'修改评论，评论id:'.$id);
        echo json_encode(array('status'=>1,'msg'=>'修改成功！'));exit;
    }


}